package Servlet;

import com.mysql.jdbc.StringUtils;
import dao.UserDao;
import pojo.Flight;
import pojo.User;
import service.FlightService;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/selectUserServlet")
public class SelectUserServlet extends HttpServlet {

    private UserDao userDao = new UserDao();
    private  UserService  userService = new  UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.  解决乱码：
        request.setCharacterEncoding("UTF-8");

        List<User> allUsers = new ArrayList<>();

        String addFlag = (String) request.getAttribute("addFlag");

        String id = request.getParameter("id");

        String age = request.getParameter("age");

        //2. 调用service查询
        if (!StringUtils.isNullOrEmpty(addFlag)  && !StringUtils.isNullOrEmpty(id)) {
      allUsers  = userService.selectid(id);
        } else if (id != null) {
           allUsers  = userService.selectid(id);
        } else {
            allUsers = userService.selectAllFlights();
        }
        //3. 存储到request中
        request.setAttribute("allUsers", allUsers);
        request.getRequestDispatcher("/userList.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}